package prj5;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Perform data analysis on files.
 * @author xinruili
 * @version 2022-04-21
 */
public class DataProcessing {
    private CSVFile csvFile;
    private List<Data> states;
    private List<String> raceNames;
    private int raceNumber;

    /**
     * Construction method.
     * @param fileName the name of file name.
     * @author xinruili
     * @version 2022-04-21
     */
    public DataProcessing(final String fileName) {
        this.csvFile = new CSVFile(fileName);
        this.states = new ArrayList<>();
        this.raceNames = new ArrayList<>();
        getRaceSequence();
        getStatesData();
    }

    /**
     * Processing of table header data.
     * Get race sequence.
     * @author xinruili
     * @version 2022-04-21
     */
    private void getRaceSequence() {
        String line = csvFile.readLines();
        String[] temp = line.split(",");
        this.raceNumber = temp.length - 1;
        this.raceNumber /= 2;
        for (int i = 0; i < raceNumber; i++) {
            String[] cells = temp[i + 1].split("_");
            this.raceNames.add(cells[1].toLowerCase());
        }
    }

    /**
     * Get the data od state.
     * @author xinruili
     * @version 2022-04-21
     */
    private void getStatesData() {
        String line = csvFile.readLines();
        while (line != null) {
            String[] temp = line.split(",");
            Data state = new Data(temp[0]);
            for (int i = 0; i < this.raceNumber; i++) {
                RaceStatusData raceData = new RaceStatusData(raceNames.get(i),
                        temp[i + 1], temp[i + 1 + raceNumber]);
                state.addRace(raceData);
            }
            states.add(state);
            line = csvFile.readLines();
        }
    }

    /**
     * Get the data of states.
     * @return the data of states.
     * @author xinruili
     * @version 2022-04-21
     */
    public List<Data> getStates() {
        return states;
    }

    /**
     * ToString
     * @return the string of states.
     * @author xinruili
     * @version 2022-04-21
     */
    @Override
    public String toString() {
        return "DataProcessing{"
                + "raceNames=" + raceNames
                + ", raceNumber=" + raceNumber
                + '}';
    }
}

/**
 * Read in csv file.
 * @author xinruili
 * @version 2022-04-21
 */
class CSVFile {
    private BufferedReader br;

    /**
     * Load file.
     * @param fileName file name.
     * @author xinruili
     * @version 1.0
     */
    CSVFile(final String fileName) {
        try {
            FileInputStream fileInputStream = new FileInputStream(fileName);
            InputStreamReader inputStream
                    = new InputStreamReader(fileInputStream);
            br = new BufferedReader(inputStream);
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * Read files.
     * Read file line by line and return as string.
     * Returns 'null' if the file has been read.
     * @return line of file
     * @author xinruili
     * @version 1.0
     */
    public String readLines() {
        String line = null;
        try {
            line = br.readLine();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return line;
    }
}
